// pages/myself/commission/qrcode.js
var network = require('../../../utils/network_util.js');
var utils = require('../../../utils/utils.js');
var api = require('../../../utils/api.js');
var Zan = require('../../../dist/index');
const app = getApp();
var timer;
var temp = 0;

Page(Object.assign({}, Zan.Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    saleInfo: null,
    balance: 0,
    notice: [],
    index: 0,
    showDialog: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true //要求小程序返回分享目标信息
    })
    // if (app.globalData.userId) {
    //   this.setData({
    //     qrcode: app.globalData.userAndTenantInfo.qrCode
    //   })
    // } else {
    //   // 由于 openId 是网络请求，可能会在 Page.onLoad 之后才返回
    //   // 所以此处加入 callback 以防止这种情况
    //   app.userAndTenantInfoCallback = res => {
    //     this.setData({
    //       qrcode: res.qrCode
    //     })
    //   }
    // }
    /**
        * 获取个人及商家信息
        */
    // if (app.globalData.userAndTenantInfo) {
    //   this.setData({
    //     balance: app.globalData.userAndTenantInfo.balance,
    //   })
    // } else {
    //   app.userAndTenantInfoCallback = res => {
    //     this.setData({
    //       balance: res.balance,
    //     })
    //   }
    // }
    network._post_json(this, api.getUserInfo, { appId: app.globalData.appId, openId: app.globalData.openId }, Osuccess);
    network._post_json(this, api.getSaleInfo, {}, success);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    temp = 0;
    if (timer) {
      clearInterval(timer)
    }
    wx.stopPullDownRefresh();
    network._post_json(this, api.getSaleInfo, {}, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.userAndTenantInfo.tenant.name,
      path: '/pages/home/home?shared=' + app.globalData.userAndTenantInfo.id,
      imageUrl: this.data.saleInfo.qrCode,
      success: function (res) {
        // 转发成功
        if (res.shareTickets) {
          wx.getShareInfo({
            shareTicket: res.shareTickets,
            success: function (res) {
              network._post_json(this, api.updateFxcs, { directShareNums: 0, directShareQunNums: 1 }, fxcsSuccess);
            },
            fail: function (res) {
              // 转发失败
              network._post_json(this, api.updateFxcs, { directShareNums: 1, directShareQunNums: 0 }, fxcsSuccess);
            }
          })
        } else {
          network._post_json(this, api.updateFxcs, { directShareNums: 1, directShareQunNums: 0 }, fxcsSuccess);
        }

      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  tixianSq: function (e) {
    if (app.globalData.userAndTenantInfo.mobile) {
      wx.navigateTo({
        url: '/pages/myself/commission/commtixian?expIntoDateStr=' + this.data.saleInfo.saleRule.expIntoDateStr + '&value2=' + this.data.saleInfo.saleRule.value2 + '&dqyongjin=' + e.currentTarget.dataset.dqyongjin,
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '还未绑定手机号，请绑定手机号',
        success: function (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/myself/info/bindMobile',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },

  sharedImg: function (e) {
    var img = e.currentTarget.dataset.img;
    var that = this;
    wx.showLoading({
      title: '',
    })
    //http://osnmq3793.bkt.clouddn.com
    var downloadTask = wx.downloadFile({
      url: api.upload + '?url=' + img,
      success: function (res) {
        wx.hideLoading();
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            wx.hideLoading();
            that.showZanToast('保存成功');
          },
          fail(res) {
            console.log(res)
            wx.hideLoading();
          }
        })
      }
    })
  },


  /**
   * 弹出框开关
   */
  toggleDialog() {
    this.setData({
      showDialog: !this.data.showDialog
    })
  },

  gonglv: function(e){
    var gonglve = e.currentTarget.dataset.bilie;
    wx.navigateTo({
      url: '/pages/myself/commission/shareGonglv?bilie=' + gonglve,
    })
  }
}))

// 注水数据
var success = function (that, res) {
  if (res.data.data) {
    if (res.data.data.saleRule) {
      if (res.data.data.saleRule.isWater == 1) {
        that.setData({
          saleInfo: res.data.data,
          notice: res.data.data.saleWaters
        })
      } else {
        that.setData({
          saleInfo: res.data.data,
          notice: res.data.data.commissions
        })
      }
      countdown(that);
    }
  }
}

var uploadSuccess = function (that, res) {

  console.log(res)
}

var fxcsSuccess = function (that, res) {
  that.showZanToast('分享成功');
}

var Osuccess = function (that, res) {
  that.setData({
    tenant: res.data.data.tenant
  })
}

function countdown(that) {
  timer = setTimeout(function () {
    that.setData({
      index: temp % that.data.notice.length
    });
    temp++;
    if (temp == that.data.notice.length) {
      temp = 0;
    }
    //console.log(temp)
    countdown(that);
  }, 3000)
}
